/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * panelArbolo.java
 *
 * Created on 17/01/2012, 17:25:40
 */
package tsb.UI.arbol;

import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import tsb.UI.PopupMenus.PopupMenuArbol;
import tsb.dominio.dispositivos.Dispositivo;
import tsb.dominio.dispositivos.coneccion.Router;
import tsb.dominio.dispositivos.coneccion.Switch;
import tsb.dominio.dispositivos.hosfinal.PC;
import tsb.dominio.dispositivos.hosfinal.Server;

/**
 *
 * @author Fede
 */
public class panelArbolo extends javax.swing.JPanel {
    
    public static final int PC=1;
    public static final int SERVER=2;
    
    public static final int ROUTER=3;
    public static final int SWITCH=4;
    public static final int MODEM=5;
    
    public static final int RJ45=6;
    public static final int FIBRA=7;
    public static final int SERIALDTE=8;
    public static final int SERIALDCE=9;
    public static final int AUTOMATICA=10;
    public static final int WIRElESS=11;
    public static final int COAXIAL=12;
    
  private  ImageIcon ICONO_ROOT = new ImageIcon(getClass().getResource("/tsb/UI/Imagenes/HOME_16x16.png"));
  private  ImageIcon ICONO_DISPOSITIVO_FINALES = new ImageIcon(getClass().getResource("/tsb/UI/Imagenes/DISPOSITIVOS_FINALES_16X16.png"));
  private  ImageIcon ICONO_DISPOSITIVO_CONECCION =new ImageIcon(getClass().getResource("/tsb/UI/Imagenes/DISPOSITIVOS_DE_CONEXION_16X16.png"));
  private ImageIcon ICONO_CONECCIONES =new ImageIcon(getClass().getResource("/tsb/UI/Imagenes/CONEXIONES_16X16.png"));
  
  private ImageIcon ICONO_PC= new  ImageIcon(getClass().getResource("/tsb/UI/Imagenes/PC_61x16.png"));
  private ImageIcon ICONO_ROUTER = new  ImageIcon(getClass().getResource("/tsb/UI/Imagenes/ROUTER_16x16.png"));
  private ImageIcon ICONO_SERVER = new  ImageIcon(getClass().getResource("/tsb/UI/Imagenes/SERVER_16x16.png"));
  private ImageIcon ICONO_SWITCH = new  ImageIcon(getClass().getResource("/tsb/UI/Imagenes/SWITCH_16x16.png"));
  private ImageIcon ICONO_MODEM = new  ImageIcon(getClass().getResource("/tsb/UI/Imagenes/MODEM_16x16.png"));
  
  private ImageIcon ICONO_RJ45 = new  ImageIcon(getClass().getResource("/tsb/UI/Imagenes/CONEXION_RJ45_16x16.png"));
  private ImageIcon ICONO_FIBRA = new  ImageIcon(getClass().getResource("/tsb/UI/Imagenes/CONEXION_FIBRA_16x16.png"));
  private ImageIcon ICONO_SERIALDCE = new  ImageIcon(getClass().getResource("/tsb/UI/Imagenes/CONEXION_SERIAL_DCE_16x16.png"));
  private ImageIcon ICONO_SERIALDTE = new  ImageIcon(getClass().getResource("/tsb/UI/Imagenes/CONEXION_SERIAL_DTE_16x16.png"));
  private ImageIcon ICONO_AUTOMATICA = new  ImageIcon(getClass().getResource("/tsb/UI/Imagenes/CONEXION_AUTOMATICA_16x16.png"));
  private ImageIcon ICONO_WIRELESS = new  ImageIcon(getClass().getResource("/tsb/UI/Imagenes/CONEXION_WIRELESS_16x16.png"));
  private ImageIcon ICONO_COAXIAL = new  ImageIcon(getClass().getResource("/tsb/UI/Imagenes/CONEXION_AUTOMATICA_16x16.png"));
  
    
  

  //protected JTree  jTree1;
  private DefaultTreeModel m_model;
  private IconCellRenderer m_renderer;
  private IconCellEditor m_editor;
  
  private DefaultMutableTreeNode nodoDispositivosFinales;
  private DefaultMutableTreeNode nodoDispositivosConeccion;
  private DefaultMutableTreeNode nodoConecciones;
  
  private Toolkit toolkit = Toolkit.getDefaultToolkit();

    /** Creates new form panelArbolo */
    public panelArbolo() {
        initComponents();
        setSize(400, 300);

        DefaultMutableTreeNode top = new DefaultMutableTreeNode(new IconData(ICONO_ROOT, "Diagrama"));
        nodoDispositivosFinales = new DefaultMutableTreeNode( new IconData(ICONO_DISPOSITIVO_FINALES, "Dispositivos Finales" ));
        nodoDispositivosConeccion = new DefaultMutableTreeNode( new IconData(ICONO_DISPOSITIVO_CONECCION, "Dispositivos Coneccion" ));
        nodoConecciones = new DefaultMutableTreeNode( new IconData(ICONO_CONECCIONES, "Conecciones" ));
      
        top.add(nodoDispositivosFinales);
        top.add(nodoDispositivosConeccion);
        top.add(nodoConecciones);
        
        //addAncestors(top);
        m_model = new DefaultTreeModel(top);
       // jTree1 = new JTree(m_model);
        jTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        jTree1.setShowsRootHandles(true); 
        jTree1.setEditable(true);

        m_renderer = new IconCellRenderer();
        jTree1.setCellRenderer(m_renderer); 
        m_editor = new IconCellEditor(jTree1);
        jTree1.setCellEditor(m_editor);
        jTree1.setInvokesStopCellEditing(true);

       // jTree1.addMouseListener(new TreeExpander());

        this.jTree1.setModel(m_model);
        
         PopupMenuArbol popup = new PopupMenuArbol(jTree1);
    }
    
    public void addDispositivosFinales(int tipoDispositivo, String nombreAMostrar)
    {
        DefaultMutableTreeNode nodo=null; 
        
        switch(tipoDispositivo)
        {
            case 1:
                nodo = new DefaultMutableTreeNode(new IconData(ICONO_PC,nombreAMostrar));
                break;
            case 2:
                nodo= new DefaultMutableTreeNode(new IconData(ICONO_SERVER,nombreAMostrar));
                break;
                
        }
        
        m_model.insertNodeInto(nodo, nodoDispositivosFinales, nodoDispositivosFinales.getChildCount());
        jTree1.expandPath(new TreePath(nodoDispositivosFinales.getPath()));
        jTree1.scrollPathToVisible(new TreePath(nodo.getPath()));
    }
    
    public void addDispositivoDeConeccion(int tipoDispositivo, String nombreAMostrar) 
     {
         DefaultMutableTreeNode nodo=null;
         switch(tipoDispositivo)
         {
             case 3:
                 nodo = new DefaultMutableTreeNode(new IconData(ICONO_ROUTER,nombreAMostrar));
                 break;                 
             case 4:
                 nodo = new DefaultMutableTreeNode(new IconData(ICONO_SWITCH,nombreAMostrar));
                 break;
             case 5:
                 nodo = new DefaultMutableTreeNode(new IconData(ICONO_MODEM,nombreAMostrar));
                 break;
         }
         
         m_model.insertNodeInto(nodo, nodoDispositivosConeccion, nodoDispositivosConeccion.getChildCount());
         jTree1.expandPath(new TreePath(nodoDispositivosConeccion.getPath()));
         jTree1.scrollPathToVisible(new TreePath(nodo.getPath()));
         
         
       
    }
     
     public void addConecciones(int tipoConeccion, String nombreAMostrar)
     {
         
          DefaultMutableTreeNode nodo= null;
          
          switch(tipoConeccion)
          {
              case 6:
                  nodo = new DefaultMutableTreeNode(new IconData(ICONO_RJ45, nombreAMostrar ));
                  break;
              case 7:
                  nodo = new DefaultMutableTreeNode(new IconData(ICONO_FIBRA, nombreAMostrar ));
                  break;
              case 8:
                  nodo = new DefaultMutableTreeNode(new IconData(ICONO_SERIALDTE, nombreAMostrar ));
                  break;
              case 9:
                   nodo = new DefaultMutableTreeNode(new IconData(ICONO_SERIALDCE, nombreAMostrar ));
                  break;
              case 10:
                  nodo = new DefaultMutableTreeNode(new IconData(ICONO_AUTOMATICA, nombreAMostrar ));
                  break;
              case 11:
                  nodo = new DefaultMutableTreeNode(new IconData(ICONO_WIRELESS, nombreAMostrar ));
                  break;
              case 12:
                  nodo = new DefaultMutableTreeNode(new IconData(ICONO_COAXIAL, nombreAMostrar ));
                  break;
          }
          
          m_model.insertNodeInto(nodo, nodoConecciones, nodoConecciones.getChildCount());
          jTree1.expandPath(new TreePath(nodoConecciones.getPath()));
          jTree1.scrollPathToVisible(new TreePath(nodo.getPath()));
          
          
     }
     
     public void removerDispositivoDeConeccion(String nombreNodo)
     {
         for(int i=0; i<nodoDispositivosConeccion.getChildCount();i++)
         {
             if(nodoDispositivosConeccion.getChildAt(i).toString().compareTo(nombreNodo)==0)
             {
                DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)  nodoDispositivosConeccion.getChildAt(i);
                 m_model.removeNodeFromParent(currentNode);
             }
         }
         toolkit.beep();
         
     }
     
     public void removerConeccion(String nombreDispositivos)
     {
          for(int i=0; i<nodoConecciones.getChildCount();i++)
         {
             if(nodoConecciones.getChildAt(i).toString().compareTo(nombreDispositivos)==0)
             {
                DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)  nodoConecciones.getChildAt(i);
                m_model.removeNodeFromParent(currentNode);
             }
         }
         toolkit.beep();
         
     }
     
     public void removerDispositivoDeFinal(String nombreNodo)
     {
         for(int i=0; i<nodoDispositivosFinales.getChildCount();i++)
         {
             if(nodoDispositivosFinales.getChildAt(i).toString().compareTo(nombreNodo)==0)
             {
                DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)  nodoDispositivosFinales.getChildAt(i);
                 m_model.removeNodeFromParent(currentNode);
             }
         }
         toolkit.beep();
         
         
         //        nodoDispositivosFinales.remove(0);
//        nodoDispositivosFinales.removeAllChildren();
//        this.jTree1.repaint();
//        jTree1.expandPath(new TreePath(nodoDispositivosFinales.getPath()));
//        jTree1.scrollPathToVisible(new TreePath(nodoDispositivosFinales.getPath()));
//        
//        toolkit.beep();
         //        TreePath currentSelection = jTree1.getSelectionPath();
//        if (currentSelection != null) {
//            DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) (currentSelection.getLastPathComponent());
//            MutableTreeNode parent = (MutableTreeNode)(currentNode.getParent());
//            if (parent != null) {
//                this.m_model.removeNodeFromParent(currentNode);
//                return;
//            }
//        } 

        // Either there was no selection, or the root was selected.
     }
     
    
     

//    private boolean addAncestors(DefaultMutableTreeNode node)
//  {
//    if (node.getChildCount() > 0)
//      return false;
//
//    Object obj = node.getUserObject();
//    if (obj == null)
//      return false;
//    node.add(new DefaultMutableTreeNode( new IconData(
//      ICONO_pc, "Father of: "+obj.toString()) ));
//    node.add(new DefaultMutableTreeNode( new IconData(
//      ICONO_pc, "Mother of: "+obj.toString()) ));
//    return true;
//  }

   
    
    class TreeExpander extends MouseAdapter 
  {
    public void mouseClicked(MouseEvent e)
    {
        System.out.println("X: "+e.getX()+" Y: "+e.getY());
      
        TreePath selPath = jTree1.getPathForLocation(e.getX(), e.getY());
        
        System.out.println(selPath.toString());
        
        System.out.println(selPath.getLastPathComponent().toString());
        
        if(e.getButton()==3)
        {
            System.out.println("click derecho");
            
        }
          
      
        
    
      }
    }
    
  
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();

        jScrollPane1.setViewportView(jTree1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree jTree1;
    // End of variables declaration//GEN-END:variables
}
